package com.jic.interaction.weCashier.service.impl;

import com.alibaba.fastjson.JSON;
import com.jic.interaction.common.utils.HttpClientUtil;
import com.jic.interaction.weCashier.model.entity.PasswordFactor;
import com.jic.interaction.weCashier.model.entity.PaymentResult;
import com.jic.interaction.weCashier.model.entity.PaymentSubmission;
import com.jic.interaction.weCashier.model.entity.PreOrder;
import com.jic.interaction.weCashier.model.entity.Refund;
import com.jic.interaction.weCashier.model.entity.SelectPreOrder;
import com.jic.interaction.weCashier.model.entity.SelectRefund;
import com.jic.interaction.weCashier.model.vo.response.PasswordFactorResponse;
import com.jic.interaction.weCashier.model.vo.response.PaymentResultResponse;
import com.jic.interaction.weCashier.model.vo.response.PaymentSubmissionResponse;
import com.jic.interaction.weCashier.model.vo.response.PreOrderResponse;
import com.jic.interaction.weCashier.model.vo.response.RefundResponse;
import com.jic.interaction.weCashier.model.vo.response.SelectPreOrderResponse;
import com.jic.interaction.weCashier.model.vo.response.SelectRefundResponse;
import com.jic.interaction.weCashier.service.PaymentSubmissionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutionException;

/**
 * @Auther YGH
 * @DATE 2020/11/11/13:44
 */

@Service
@Slf4j
public class PaymentSubmissionServiceImpl implements PaymentSubmissionService {


    /*
     * @Author chengpunan
     *@Package com.jic.interaction.cardCouponPlatform.service.impl
     *ClassName CardCouponPlatformServiceImpl
     * @Description 下单 //TODO topip: bcces-wbms.0001 调用方式不确定
     * @Date 16:19 2020/11/11
     * @Param CardCouponPlatform 采购商
     * @return 订单编号
     * @Version:1.0
     **/

    /**
     * @Author gym
     * @param paymentSubmission
     * @return
     */
    @Override
    public PaymentSubmissionResponse addPlaceAnOrder(PaymentSubmission paymentSubmission) {
        return null;
    }

    /**
     * @Author gym
     * @param paymentResult
     * @return
     */
    @Override
    public PaymentResultResponse getOrderCheck(PaymentResult paymentResult) throws InterruptedException, ExecutionException, IOException {
        String url ="https://svrapi.test.webank.com/m/acq/v2/wepay/makeOrderCheck";
        Map<String,String> param = JSON.parseObject(JSON.toJSONString(paymentResult), Map.class);
        log.debug("==========【请求参数】==========",JSON.toJSONString(param));
        Map<String,String> headersMap = new HashMap<>();
//        String body =  HttpClientUtil.doRequestPost(url, param, headersMap);
//        log.debug("==========【返回结果】==========",JSON.toJSONString(body));
        return null;
    }

    /**
     * 退款
     * @param refund
     * @return
     */
    @Override
    public RefundResponse deleteOrder(Refund refund) {
        String url ="https://svrapi.test.webank.com/m/acq/v2/wepay/refundOrder";
        Map<String, String> param = JSON.parseObject(JSON.toJSONString(refund), Map.class);
        log.debug("==========【请求参数】==========", JSON.toJSONString(param));
        Map<String, String> headersMap = new HashMap<>();
//            String body = HttpClientUtil.doRequestPost(url, param, headersMap);
//            log.debug("==========【返回结果】==========", JSON.toJSONString(body));
        return null;
    }

    /**
     * 获取密码因子
     * @param passwordFactor
     * @return
     */
    @Override
    public PasswordFactorResponse getPassWord(PasswordFactor passwordFactor) {
        String url ="https://svrapi.test.webank.com/m/acq/v2/wepay/getPwdFactor";
        Map<String, String> param = JSON.parseObject(JSON.toJSONString(passwordFactor), Map.class);
        log.debug("==========【请求参数】==========", JSON.toJSONString(param));
        Map<String, String> headersMap = new HashMap<>();
        headersMap.put("Content-Type", "application/json");
//            String body = HttpClientUtil.doRequestPost(url, param, headersMap);
//            log.debug("==========【返回结果】==========", body);
        return null;
    }

    /**
     * 退款查询
     * @param selectRefund
     * @return
     */
    @Override
    public SelectRefundResponse selectRefund(SelectRefund selectRefund) {
        String url ="https://svrapi.test.webank.com/m/acq/v2/wepay/refundOrderCheck";
        Map<String, String> param = JSON.parseObject(JSON.toJSONString(selectRefund), Map.class);
        log.debug("==========【请求参数】==========", JSON.toJSONString(param));
        Map<String, String> headersMap = new HashMap<>();
        headersMap.put("Content-Type", "application/json");
//            String body = HttpClientUtil.doRequestPost(url, param, headersMap);
//            log.debug("==========【返回结果】==========", JSON.toJSONString(body));
//            return null;

        return null;
    }

    /**
     * 预下单
     * @param preOrder
     * @return
     */
    @Override
    public PreOrderResponse insertPreOrder(PreOrder preOrder) {
        String url ="http://svrapi.test.webank.com/m1/acq/v2/wepay/preMakeOrder";
        Map<String, String> param = JSON.parseObject(JSON.toJSONString(preOrder), Map.class);
        log.debug("==========【请求参数】==========", JSON.toJSONString(param));
        Map<String, String> headersMap = new HashMap<>();
        headersMap.put("Content-Type", "application/json");
        String body = null;
        try {
            body = HttpClientUtil.doRequestPost(url, param, headersMap);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        log.debug("==========【返回结果】==========", JSON.toJSONString(body));
        PreOrderResponse response = new PreOrderResponse();
        response.setCode("1111");
        response.setMsg("调试测试成功");
        response.setBizSeqNo("123");
        return response;
    }

    @Override
    public SelectPreOrderResponse selectPreOrder(SelectPreOrder selectPreOrder) {
        String url ="https://open.test.webank.com/k/api/acq/v2/wepay/preMakeOrderCheck";
        Map<String, String> param = JSON.parseObject(JSON.toJSONString(selectPreOrder), Map.class);
        log.info("==========【请求参数】==========", JSON.toJSONString(param));
        Map<String, String> headersMap = new HashMap<>();
        headersMap.put("Content-Type", "application/json");
//            String body = HttpClientUtil.doRequestPost(url, param, headersMap);
//            log.info("==========【返回结果】==========", JSON.toJSONString(body));
        return null;
    }
}
